package com.koomii.common.ws;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * 返回结果
 * 
 * datatable 专用 返回结果
 * 
 */
public class Result implements Serializable {
	
	private static final long serialVersionUID = 1998428067166208629L;

	/**
	 * 结果码
	 */
	private String code;

	/**
	 * 描述
	 */
	private String description;

	/**
	 * 成功标志
	 */
	private boolean isSuccess;

	/**
	 * 自定义属性
	 */
	private Map<String, Object> resultMap = new HashMap<String, Object>();

	public Result() {

	}

	public Result(String resultCode, String description) {
		this.code = resultCode;
		this.description = description;
	}

	public Result(String resultCode, String description, boolean isSuccess) {
		this(resultCode,description);
		this.isSuccess = isSuccess;
	}
	
	public void setPage(Integer size, Collection<?> e){
		setProperty("iTotalDisplayRecords", size);
		setProperty("rows", e);
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Map<String, Object> getResultMap() {
		return resultMap;
	}

	public void setResultMap(Map<String, Object> resultMap) {
		this.resultMap = resultMap;
	}

	public boolean isSuccess() {
		return isSuccess;
	}

	public void setSuccess(boolean isSuccess) {
		this.isSuccess = isSuccess;
	}

	/**
	 * 获取结果中某个属性的值
	 * 
	 * @param propertyName 属性的名称
	 * @return 属性的值
	 */
	public Object getProperty(String propertyName) {
		return this.resultMap.get(propertyName);
	}

	/**
	 * 设置某个属性
	 * 
	 * @param propertyName 属性的名称
	 * @param propertyValue 属性的值
	 * @return 如果已经存在属性，则返回的是现有的属性值；否则返回null
	 */
	public Object setProperty(String propertyName, Object propertyValue) {
		return this.resultMap.put(propertyName, propertyValue);
	}
	
	public void removeProperty(String propertyName){
		this.resultMap.remove(propertyName);
	}
	
	public void removeAllProperty(){
		this.resultMap.clear();
	}
	
	public void setCode(String resultCode,String description){
		setCode(resultCode);
		setDescription(description);
	}

	@Override
	public String toString() {
		return "Result [code=" + code + ", description=" + description + ", isSuccess=" + isSuccess + ", resultMap=" + resultMap + "]";
	}

}
